﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using Film.Repository;
using Film.Common;

namespace PhuongDoAn.Admin.Age
{
    public partial class Edit : AdminWebBase
    {
        public int ID = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            ////tinh thang ID dua tren query tim duoc!
            if (Request.QueryString["ItemID"] != null)
            {
                int.TryParse(Request.QueryString["ItemID"].ToString(), out ID);
            }

            if (!IsPostBack && !X.IsAjaxRequest)
            {
                try
                {
                    InitData();
                }
                catch (Exception ex)
                {
                    THONG_BAO_CAP_NHAT("Lỗi : " + ex.Message);
                }
            }
        }

        private void InitData()
        {
            if (ID == 0)
            {
                btnCapNhat.Disabled = true;
                txtName.Focus(true, 100);
            }
            if (ID > 0)
            {
                txtName.Focus(true, 100);
                InitLoadAction();
            }
        }



        private void InitLoadAction()
        {
            ////thuc hien tim van ban o day va thuc hien
            tblAgeRepository countryRepository = new tblAgeRepository();
            tblAge entity = countryRepository.GetByAgeID(ID);
            if (entity == null)
            {
                //Dong lai cai form nay luon
                Page.Response.Redirect(ConfigKey.ERROR_URL);
                return;
            }
            txtName.Text = HttpUtility.HtmlEncode(entity.AgeName);

        }

        public void onSave(object sender, DirectEventArgs e)
        {
            try
            {
                tblAgeRepository countryRepository = new tblAgeRepository();
                tblAge entity = new tblAge();
                if (ID > 0)
                {
                    entity = countryRepository.GetByAgeID(ID);
                }
                entity.AgeName = txtName.Text.Trim();

                if (ID == 0)
                {
                    entity.AgeID = countryRepository.GetNextID();
                    countryRepository.Add(entity);
                }
                else
                {

                   // countryRepository.Update(entity);
                }
                countryRepository.SubmitChanges();

                ResourceManager1.RegisterClientScriptBlock("hideForm", "ReloadAddOrEdit();");
            }
            catch (Exception ex)
            {
                THONG_BAO_CAP_NHAT(" Lỗi : " + ex.Message);

            }
        }


        protected override void OnPreRender(EventArgs e)
        {
            if (!IsPostBack)
            {
                showLanguage();
            }
        }

        private void showLanguage()
        {

            btnCapNhat.DirectEvents.Click.EventMask.Msg = "Đang cập nhật dữ liệu, vui lòng chờ...";
            btnCapNhat.Text = "Lưu";
            ValidationStatus1.HideText = "Click để ẩn thông báo";
            ValidationStatus1.ShowText = "[ Dữ liệu nhập chưa đúng ]";
        }
    }
}